import {legacy_createStore,applyMiddleware} from "redux"
import thunk from "redux-thunk"
import logger from "redux-logger"
import {APILIST,MYSTATE,ALLLIST,ADDFN} from "../types/home.d"
const initialState = {
     datalist :[]
}

const reducer= (state :MYSTATE= initialState, { type, payload }:any) => {
    let newarr=JSON.parse(JSON.stringify(state.datalist))
  switch (type) {
      
  case ALLLIST:
      newarr=payload
    return { ...state,
    datalist:newarr
    }
    case ADDFN :
        newarr.forEach((value:APILIST, index:number) => {
             if(value.id==payload.id){
               if(!value.car){
                   value.car=true
               }else {
                   value.num++
               }

             }
        })
        return {
            ...state,
            datalist:newarr
        }

  default:
    return {...state}
  }
}

const store = legacy_createStore(reducer,applyMiddleware(thunk,logger))
export default store
